using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Demo.Model;
using Pampa.GenericEntity;

public partial class Test_GenericEntityEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request["id"];
        string type = Request["type"];
        string onSuccess = Request["onSuccess"];

        GenericEntity genericEntity;

        if (String.IsNullOrEmpty(id))
        {
            genericEntity = new GenericEntity();
            genericEntity.Type = type;

            this.EntityEdit1.Entity = genericEntity;
            this.EntityEdit1.LoadConfiguration(type);
        }
        else
        {
            genericEntity = GenericEntity.Find(int.Parse(id));
            this.EntityEdit1.Entity = genericEntity;
            this.EntityEdit1.LoadConfiguration(type);
        }

        // Al crear un customer tambien debe crearse su address
        if ((type == "Customer") && (genericEntity.GetPropertyValue("Address") == null))
        {
            genericEntity.SetPropertyValue("Address", new GenericEntity());
        }

        if(onSuccess == "GenericCustomerGrid")
            this.EntityEditSaveButton1.OnSuccess = "window.location = 'GenericCustomerGrid.aspx'";
        else
            this.EntityEditSaveButton1.OnSuccess = "window.location = 'GenericEntityGrid.aspx'";

    }
}
